package cn.edu.zjicm.wordsnet_d.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.base.WordBaseActivity;
import cn.edu.zjicm.wordsnet_d.data.StaticData;
import cn.edu.zjicm.wordsnet_d.data.StudyPlan;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference.BookType;
import cn.edu.zjicm.wordsnet_d.db.WordFactory;
import cn.edu.zjicm.wordsnet_d.util.LogUtil;
import cn.edu.zjicm.wordsnet_d.view.SettingPlanDialog;

/**
 * 选择学习安排方式
 * 
 * @author cnting
 * 
 */
public class SelectStudyModeActivity extends WordBaseActivity {

	private TextView bookTv;
	private LinearLayout endTimeLayout, numberLayout;
	private WordFactory mFactory;

	private int bookIndex;
	private String bookClass;
	private int unlearnWordCount;
	private int startMode; // 开始模式
	private int bookType; // 学习单词书的类型

	public static SelectStudyModeActivity mContext;

	public static void startActivity(Context context, int bookIndex, String bookClass, int bookType) {
		Intent in = new Intent(context, SelectStudyModeActivity.class);
		in.putExtra("bookIndex", bookIndex);
		in.putExtra("bookClass", bookClass);
		in.putExtra("bookType", bookType);
		context.startActivity(in);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_study_mode);
		mContext = this;
		findView();
		initView();
	}

	private void findView() {
		bookTv = (TextView) findViewById(R.id.book_tv);
		endTimeLayout = (LinearLayout) findViewById(R.id.setEndTimeLayout);
		numberLayout = (LinearLayout) findViewById(R.id.setNumberLayout);
	}

	private void initView() {
		mFactory = WordFactory.getInstance(this);
		bookIndex = getIntent().getIntExtra("bookIndex", -1);
		bookClass = getIntent().getStringExtra("bookClass");
		bookType = getIntent().getIntExtra("bookType", BookType.BOOK_WORD);
		startMode = getIntent().getIntExtra("startMode", StaticData.MODE_INIT);
		unlearnWordCount = mFactory.getUnLearnedWordCount(bookIndex);

		setTitleBarStyle(TITLE_BAR_STYLE_BACK_BUTTON);
		setTitleText("安排学习计划");

		StringBuilder sb = new StringBuilder();
		if (bookClass != null && bookClass.length() > 0) {
			sb.append(bookClass);
			sb.append(" - ");
		}

		sb.append(mFactory.getBookName(AppPreference.Book.BOOK_ID[bookIndex]));
		sb.append("(");
		sb.append(unlearnWordCount);
		sb.append("词)");

		bookTv.setText(sb.toString());

		endTimeLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 这里暂时书的类别用单词
				new SettingPlanDialog(SelectStudyModeActivity.this, AppPreference.StudyMode.MODE_END_TIME, bookType, bookIndex, unlearnWordCount);
			}
		});
		numberLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 这里暂时书的类别用单词
				new SettingPlanDialog(SelectStudyModeActivity.this, AppPreference.StudyMode.MODE_NUMBER, bookType, bookIndex, unlearnWordCount);
			}
		});
	}

	public void showPlanDetail(StudyPlan studyPlan) {
		if (startMode == StaticData.MODE_INIT) {
			SettingPlanActivity.startActivity(this, StaticData.MODE_INIT, bookTv.getText().toString(), studyPlan);
		} else {
			Intent in = new Intent();
			in.putExtra("studyPlan", studyPlan);
			LogUtil.d("studyPlan:" + studyPlan.toString());
			this.setResult(StaticData.resultCode, in);
			finish();
		}
	}

	public void toFinish() {
		finish();
		mContext = null;
	}
}
